﻿using Dapper;
using System;
using BCEntity.TMS.OrderSpecificServiceRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.Order.OrderSpecificServiceRecord
{
    /// <summary>
    /// 订单特殊服务记录
    /// </summary>
    public class OrderSpecificServiceRecordData : IOrderSpecificServiceRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM orderspecificservicerecord";
        private const string QUERY = @"SELECT OrderSpecificServiceRecordId,HasUnit,ServiceUnit,CreateTime,OrderId,OrderNo,TransportSpecificServiceConfigId,SpecificServiceType,ServiceTitle,HasRemark,ServiceRemark,ServiceAmount FROM orderspecificservicerecord";
        public OrderSpecificServiceRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查订单特殊服务记录是否存在
        public async Task<bool> Exists(long orderSpecificServiceRecordId)
        {
            string sql = string.Format(@"{0} WHERE OrderSpecificServiceRecordId=?OrderSpecificServiceRecordId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderSpecificServiceRecordId", orderSpecificServiceRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加订单特殊服务记录
        public async Task<OrderSpecificServiceRecordEntity> Add(OrderSpecificServiceRecordEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("orderspecificservicerecord", new string[] { "HasUnit", "OrderId", "OrderNo", "TransportSpecificServiceConfigId", "SpecificServiceType", "ServiceTitle", "HasRemark", "ServiceAmount" });
            sql = string.Format("{0} {1} WHERE OrderSpecificServiceRecordId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?HasUnit", entity.HasUnit, DbType.Boolean);
            parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TransportSpecificServiceConfigId", entity.TransportSpecificServiceConfigId, DbType.Int32, size: 11);
            parameters.Add("?SpecificServiceType", entity.SpecificServiceType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ServiceTitle", entity.ServiceTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?HasRemark", entity.HasRemark, DbType.Boolean);
            parameters.Add("?ServiceAmount", entity.ServiceAmount, DbType.Decimal, size: 10);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderSpecificServiceRecordEntity>(sql, parameters, databaseContext.Transaction);

        }
        public async Task<bool> Add(List<OrderSpecificServiceRecordEntity> entities)
        {
            return await this.databaseContext.Master.InsertAllAsync(entities, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 修改订单特殊服务记录
        public async Task<OrderSpecificServiceRecordEntity> Update(OrderSpecificServiceRecordEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("orderspecificservicerecord", new string[] { "HasUnit", "ServiceUnit", "OrderId", "OrderNo", "TransportSpecificServiceConfigId", "SpecificServiceType", "ServiceTitle", "HasRemark", "ServiceRemark", "ServiceAmount" }, new string[] { "OrderSpecificServiceRecordId" });
            sql = string.Format("{0} {1} WHERE OrderSpecificServiceRecordId=?OrderSpecificServiceRecordId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderSpecificServiceRecordId", entity.OrderSpecificServiceRecordId, DbType.Int64, size: 20);
            parameters.Add("?HasUnit", entity.HasUnit, DbType.Boolean);
            parameters.Add("?ServiceUnit", entity.ServiceUnit?.Trim(), DbType.AnsiString, size: 10);
            parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
            parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TransportSpecificServiceConfigId", entity.TransportSpecificServiceConfigId, DbType.Int32, size: 11);
            parameters.Add("?SpecificServiceType", entity.SpecificServiceType?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ServiceTitle", entity.ServiceTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?HasRemark", entity.HasRemark, DbType.Boolean);
            parameters.Add("?ServiceRemark", entity.ServiceRemark?.Trim(), DbType.AnsiString, size: 200);
            parameters.Add("?ServiceAmount", entity.ServiceAmount, DbType.Decimal, size: 10);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderSpecificServiceRecordEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除订单特殊服务记录
        public async Task<bool> Delete(long orderSpecificServiceRecordId)
        {
            string sql = @"DELETE FROM orderspecificservicerecord WHERE OrderSpecificServiceRecordId=?OrderSpecificServiceRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderSpecificServiceRecordId", orderSpecificServiceRecordId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取订单特殊服务记录
        public async Task<OrderSpecificServiceRecordEntity> GetEntity(long orderSpecificServiceRecordId)
        {
            string sql = string.Format(@"{0} WHERE OrderSpecificServiceRecordId=?OrderSpecificServiceRecordId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?OrderSpecificServiceRecordId", orderSpecificServiceRecordId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderSpecificServiceRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取订单特殊服务记录列表
        public async Task<IEnumerable<OrderSpecificServiceRecordEntity>> GetList(string orderNo)
        {
            string sql = string.Format("{0} WHERE OrderNo=?OrderNo", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("OrderNo", orderNo, DbType.AnsiString);
            return await this.databaseContext.Master.QueryAsync<OrderSpecificServiceRecordEntity>(sql, parameters);
        }
        public async Task<IEnumerable<OrderSpecificServiceRecordEntity>> GetList(long orderId)
        {
            string sql = string.Format("{0} WHERE OrderId=?OrderId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("OrderId", orderId, DbType.Int64);
            return await this.databaseContext.Master.QueryAsync<OrderSpecificServiceRecordEntity>(sql, parameters);
        }
        #endregion

        #region 获取订单特殊服务记录分页
        public async Task<(IEnumerable<OrderSpecificServiceRecordEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM orderspecificservicerecord {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.QueryMultipleAsync<OrderSpecificServiceRecordEntity, int>(sql, parameters);
        }
        #endregion
    }
}



